package com.sj.generator;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.IColumnType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

/**
 * @author swallow
 * Created by swallow on 2019/2/7.<br>
 */
public class GenCode {

    public static void main(String[] args) {
        AutoGenerator mpg = new AutoGenerator();
        // 读取配置文件
        ResourceBundle bundle = ResourceBundle.getBundle("mybatis-plus");
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(bundle.getString("outPutDir"));
        gc.setOpen(false);
        gc.setBaseResultMap(true);
        gc.setBaseColumnList(true);
        gc.setAuthor(bundle.getString("author"));
        gc.setMapperName("%sMapper");
        gc.setXmlName("%sMapper");
        gc.setServiceName("I%sService");
        gc.setServiceImplName("%sServiceImpl");
        gc.setControllerName("%sController");

        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);
        dsc.setTypeConvert(new MySqlTypeConvert(){
            @Override
            public IColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
                String t = fieldType.toLowerCase();
                if(t.contains("tinyint(1)")){
                    return DbColumnType.BASE_SHORT;
                }
                if(t.contains("date")){
                    return DbColumnType.DATE;
                }
                return super.processTypeConvert(globalConfig, fieldType);
            }
        });
        dsc.setDriverName(bundle.getString("driverClassName"));
        dsc.setUrl(bundle.getString("url"));
        dsc.setUsername(bundle.getString("username"));
        dsc.setPassword(bundle.getString("password"));
        mpg.setDataSource(dsc);


        // 包配置
        String className = bundle.getString("className");
        PackageConfig pc = new PackageConfig();
        pc.setParent(bundle.getString("packageParent"));
        pc.setController(className+".controller");
        pc.setService(className+".service");
        pc.setServiceImpl(className+".service.impl");
        pc.setMapper(className+".dao");
        pc.setEntity(className+".entity");
        mpg.setPackageInfo(pc);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // TODO
            }
        };

        List<FileOutConfig> focList = new ArrayList<>();
        focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return bundle.getString("outPutDirXml")+"/mapper/"+className+"/"+tableInfo.getEntityName()+ StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);
        mpg.setTemplate(new TemplateConfig().setXml(null));

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel)
                .setColumnNaming(NamingStrategy.underline_to_camel)
                .setEntityLombokModel(true)
                .setRestControllerStyle(true)
                .entityTableFieldAnnotationEnable(false)
                .setInclude(new String[]{bundle.getString("tableNames")});
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }

}
